import { Block } from "./Block";
import { Blockchain } from "./Blockchain";
import yargs from 'yargs'
import { P2PServer } from "./P2PServer";

// 解析命令行参数
const argv = yargs.option('dataPath', {
    alias: 'd',
    description: 'Blockchain data file path',
    type: 'string'
})
.option('port', {
    alias: 'p',
    description: 'P2P server port',
    type: 'number'
})
.option('host', {
    alias: 'h',
    description: 'P2P server host',
    type: 'string'
})
.option('seedPeers', {
    alias: 'sp',
    description: 'P2P server seed peers',
    type: 'string'
})
.help().alias('help', 'h').argv;

// 获取区块链数据文件存储路径
const dataPath = (argv as any).dataPath || './data/blockchain.json';
const port = (argv as any).port || 12315;
const host = (argv as any).host || 'localhost';
const seedPeers = (argv as any).seedPeers || ['localhost:12315'];

const myBlockchain = new Blockchain(dataPath);

const myServer = new P2PServer(myBlockchain, port, host, seedPeers);
myServer.listen()

myBlockchain.addBlock(
    new Block(
        1,
        '2024-04-18',
        JSON.stringify({
            amount: 4,
        })
    )
);

myBlockchain.addBlock(
    new Block(
        2,
        '2024-04-18',
        JSON.stringify({
            amount: 10,
        })
    )
);

// myBlockchain.addBlock(
//     new Block(
//         3,
//         '2024-04-19',
//         JSON.stringify({
//             amount: 20,
//         })
//     )
// );
